package com.dofast.bt.mvp.view.activity.message;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.Loader;
import android.view.KeyEvent;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.dofast.bt.R;
import com.dofast.bt.base.BaseMvpActivity;
import com.dofast.bt.mvp.presenter.message.WebViewPresenter;
import com.dofast.bt.util.PresenterFactory;
import com.dofast.bt.util.PresenterLoder;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * username:Created by hsj on 2016/12/25 0025.
 * class：
 * 类描述：网页
 */

public class WebViewActivity2 extends BaseMvpActivity<WebViewPresenter, IWebView> implements IWebView {
    @BindView(R.id.webview)
    WebView webview;

    @Override
    protected int getResourcesId() {
        return R.layout.activity_webview;
    }

    @Override
    protected void initView() {
        presenter.intData();
//如果访问的页面中有Javascript，则webview必须设置支持Javascript
        WebSettings settings = webview.getSettings();
        settings.setJavaScriptEnabled(true);

        WebSettings ws = webview.getSettings();
        ws.setBuiltInZoomControls(true);// 隐藏缩放按钮

        ws.setUseWideViewPort(true);// 可任意比例缩放
        ws.setLoadWithOverviewMode(true);// setUseWideViewPort方法设置webview推荐使用的窗口。setLoadWithOverviewMode方法是设置webview加载的页面的模式。

        ws.setSavePassword(true);
        ws.setSaveFormData(true);// 保存表单数据

        ws.setDomStorageEnabled(true);
        ws.setSupportMultipleWindows(true);// 新加

        //我就是没有这一行，死活不出来。MD，硬是没有人写这一句的
        webview.setWebChromeClient(new WebChromeClient());
        //覆盖WebView默认使用第三方或系统默认浏览器打开网页的行为，使网页用WebView打开
        webview.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                //返回值是true的时候控制去WebView打开，为false调用系统浏览器或第三方浏览器
                presenter.showWeb();
                return true;
            }
        });
//优先使用缓存
        webview.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
    }

    @Override
    protected void addOnClickListener() {

    }

    @Override
    public Loader<WebViewPresenter> onCreateLoader(int id, Bundle args) {
        return new PresenterLoder<>(this, new PresenterFactory<WebViewPresenter>() {
            @Override
            public WebViewPresenter create() {
                return new WebViewPresenter();
            }
        });
    }

    @Override
    public void initTitle() {

    }

    @Override
    public void gotoActivity(Class<?> tClass, boolean isFinish) {

    }

    @Override
    public void showWeb(String url) {
       webview.loadData(url,"text/html","UTF-8");
        // webview.loadUrl(url);
    }

    public static void gotoWeb(Context context, String url) {
        Intent intent = new Intent(context, WebViewActivity2.class);
        intent.putExtra("address", url);
        context.startActivity(intent);
    }

    @Override
    public String getUrl() {
        return getIntent().getStringExtra("address");
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    //改写物理按键——返回的逻辑
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (webview.canGoBack()) {
                webview.goBack();//返回上一页面
                return true;
            } else {
                finish();
            }
        }
        return super.onKeyDown(keyCode, event);
    }
    @Override
    protected void onPause ()
    {
        webview.reload ();

        super.onPause ();
    }
}
